﻿using AnlianCommon.Common.Domains.BreedingManagement;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.SheepCommands.PhaseDeterminationes
{
    public class PhaseDeterminationQueryHandler : IRequestHandler<PhaseDeterminationQueryCommand, ResultPage<PhaseDetermination>>
    {
        private readonly IBaseRepository<PhaseDetermination> _phaseDeterminationRepository;

        public PhaseDeterminationQueryHandler(IBaseRepository<PhaseDetermination> phaseDeterminationRepository)
        {
            _phaseDeterminationRepository = phaseDeterminationRepository;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<PhaseDetermination>> Handle(PhaseDeterminationQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<PhaseDetermination>() { Code = (int)CommonErrorCode.Search, TotalCount = 0, PagedCount = 0 };

            try
            {
                var phaseDetermination = await (from a in await _phaseDeterminationRepository.GetAll()
                                                select new PhaseDetermination()
                                                {
                                                    PhaseDeterminationId = a.PhaseDeterminationId,
                                                    SheepNumber = a.SheepNumber,
                                                    Variety = a.Variety,
                                                    SheepSex = a.SheepSex,
                                                    CurrentHouse = a.CurrentHouse,
                                                    CurrentField = a.CurrentField,
                                                    CurrentDate = a.CurrentDate,
                                                    CurrentDay = a.CurrentDay,
                                                    OcclusionTeeth = a.OcclusionTeeth,
                                                    SheepHair = a.SheepHair,
                                                    SheepSpot = a.SheepSpot,
                                                    Hoofolor = a.Hoofolor,
                                                    MouthColor = a.MouthColor,
                                                    SheepAngle = a.SheepAngle,
                                                    DistributionFat = a.DistributionFat,
                                                    CreatedOwnerId = a.CreatedOwnerId,
                                                    CreatedTime = a.CreatedTime,
                                                }).ToListAsync();
                if (!string.IsNullOrEmpty(request.SheepNumber))
                {
                    phaseDetermination = phaseDetermination.Where(c => c.SheepNumber == request.SheepNumber).ToList();
                }
                if (!string.IsNullOrEmpty(request.Variety))
                {
                    phaseDetermination = phaseDetermination.Where(c => c.Variety == request.Variety).ToList();
                }
                if (request.SheepSex != null)
                {
                    phaseDetermination = phaseDetermination.Where(c => c.SheepSex == request.SheepSex).ToList();
                }
                result.TotalCount = phaseDetermination.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                phaseDetermination = phaseDetermination.OrderBy(c => c.PhaseDeterminationId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = phaseDetermination;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
